package cn.ml.web1662.web.filter;

import javax.servlet.*;
import java.io.IOException;

public class CharEncodingFilter implements Filter {
    private String encode = "utf-8";
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
       String en = filterConfig.getInitParameter("encode");
       if(en !=null ){
           encode = en;
       }
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding(encode);
        servletResponse.setContentType("text/html;charset="+encode);
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
